/*
* Copyright (C) 2008-2009 J-P Nurmi jpnurmi@gmail.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* $Id: server.h 293 2009-02-15 09:52:20Z vjousse $
*/

#ifndef SERVER_H
#define SERVER_H

#include <QUrl>
#include <QDebug>
#include <QString>
#include <QVariant>
#include <QMetaType>
#include <QDataStream>

struct Server
{
    Server(const QString& name = QString()) : name(name), autoConnect(false) { }

    Server& operator=(const Server& other)
    {
        if (this != &other)
        {
            name = other.name;
            host = other.host;
            port = other.port;
            user = other.user;
            password = other.password;
            autoConnect = other.autoConnect;
        }
        return *this;
    }

    operator QVariant() const
    {
        return QVariant::fromValue(*this);
    }
    
    bool operator==(const Server& other) const
    {
        return name == other.name
            && host == other.host
            && user == other.user
            && password == other.password
            && port == other.port
            && autoConnect == other.autoConnect;
    }
    
    bool isNull() const
    {
        return host.isNull();
    }
    
    QUrl toUrl() const
    {
        QUrl url;
        url.setHost(host);
        url.setUserName(user);
        url.setPassword(password);
        url.setPort(port);
        return url;
    }
    
    static Server fromUrl(const QUrl& url)
    {
        Server server;
        server.name = url.host();
        server.host = url.host();
        server.user = url.userName();
        server.password = url.password();
        server.port = url.port();
        return server;
    }

	QString name;
	QString host;
    QString user;
	QString password;
	quint16 port;
    bool autoConnect;
};

typedef QList<Server> ServerList;
Q_DECLARE_METATYPE(Server)
Q_DECLARE_METATYPE(ServerList)

static inline QDebug operator<<(QDebug debug, const Server& server)
{
    debug.nospace() << "Server("
            << server.name << ","
            << server.host << ","
            << server.user << ","
            << server.password << ","
            << server.port << ","
            << server.autoConnect << ")";
    return debug.space();
}

static inline QDataStream& operator<<(QDataStream& out, const Server& server)
{
    out << server.name << server.host << server.user << server.password << server.port << server.autoConnect;
    
	return out;
}

static inline QDataStream& operator>>(QDataStream& in, Server& server)
{
    in >> server.name >> server.host >> server.user >> server.password >> server.port >> server.autoConnect;

	return in;
}

#endif // SERVER_H
